<?php
class indexController extends adminController{
    protected $layout = 'layout';
    public function __construct(){
        if (!defined('IS_LOGIN')) $this->pageError();   
        parent::__construct();
    }
    //管理首页
    public function index(){
        $this->leftMenu = api(getApps(), 'getMenu');  //左侧管理菜单
        $this->nav = api(getApps(), 'getHeder');  //顶部菜单
        $this->display();
    }
    //APP菜单管理
    public function menu(){
        $this->layout = null;
        $nav = $_GET['nav'];
        $menu = api(getApps(), 'getNav');
        $leftMenu = $menu[$nav];
        if(!$leftMenu){
            $this->leftMenu = array(1 => array('title'=>'404 Error'));
        }else{
            $this->leftMenu = $leftMenu;
        }
        $this->display();
    }   
    //右边
    public function welcome(){
        $this->title = config('APP_NAME');
        $this->config = Config();
        $this->login_info = $_SESSION[$this->appID . '_userInfo'];
        $this->display();
    }
    //管理员列表
    public function user(){
        $act = in($_GET['act']);
        if($act == 'del'){
            $fid = intval($_GET['fid']);
            if($fid == 1){
                echo 'ID是“1”的系统管理员禁止删除!';
                exit();
            } 
            $condition['id'] = $fid;
            $result =  model('admin')->delete($condition);
            if($result){
                echo 1;
            }else{
                echo '删除失败!';
            } 
        }else{
            $this->info = model('admin')->select('','','id desc');
            $this->display();
        }
    }
    //管理员列表
    public function adduser(){
        if($this->isPost()){
            $fid = trim($_POST['fid']);
            $username = trim($_POST['username']);
            $password = trim($_POST['password']);
            $chackpassword = trim($_POST['chackpassword']);
            if($fid){
                $msg = Check::rule(array(Check::must($username),'请输入用户名'));
                if($password){
                    $msg = Check::rule(
                        array(Check::must($chackpassword), '请输入确认密码'),
                        array(check::same($password,$chackpassword),'两次密码输入不相同'));
                    if(true !== $msg){
                        $this->jsonMsg($msg);
                    }
                    $data['password'] = md5($password);
                }
                $data['username'] = $username;
                $condition['id'] = $fid;
                $rel = model('admin')->update($condition,$data);
            }else{
                $msg = Check::rule(
                    array(Check::must($username),'请输入用户名'),
                    array(Check::must($password),'请输入密码'),
                    array(Check::must($chackpassword),'请输入密码'),
                    array(check::same($password,$chackpassword),'两次密码输入不相同'));
                if(true !== $msg){
                    $this->jsonMsg($msg);
                }
                $data['username'] = $username;
                $data['password'] = md5($password);
                $data['login_time'] = time();
                $rel = model('admin')->insert($data);
            }
            if($rel){
                $this->jsonMsg('提您的信息提交成功',1);
            }else{
                $this->jsonMsg('提交设置失败,请返回重试');
            }
        }else{
            $this->fid = $_GET['fid'];
            $condition['id'] = $this->fid;
            $this->info = model('admin')->find($condition,'username');
            $this->display();
        }
    }
    //更改密码
    public function password(){
        if($this->isPost()){
            $password = trim($_POST['password']);
            $newpassword = trim($_POST['newpassword']);
            $chackpassword = trim($_POST['chackpassword']);
            $msg = Check::rule(
                array(Check::must($password), '请输入现在的密码') ,
                array(Check::must($newpassword), '请输入新密码'),
                array(Check::must($chackpassword), '请输入确认密码'),
                array(check::same($newpassword,$chackpassword),'两次新密码输入不相同'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $condition['id'] = $_SESSION['admin_userInfo']['id'];
            $Info = model('admin')->find($condition,'password');
            $password = md5($password);
            if(!empty($Info) && $Info['password'] == $password){
                $data['password'] = md5($newpassword);
                model('admin')->update($condition,$data);
                 $this->jsonMsg('密码修改成功',1);
            }else{
                $this->jsonMsg('修改失败,原始密码错误.');
            }
        }else{
            $this->login_info = $_SESSION[$this->appID . '_userInfo'];
            $this->display();
        }   
    }
    //登录
    public function login(){
        session_start();
        if($this->isPost()){
            $username = trim($_POST['username']);
            $password = trim($_POST['password']);
            $msg = Check::rule(
                    array( Check::must($username), '请输入用户名') ,
                    array( Check::must($password), '请输入密码'));
            if(true !== $msg){
                $this->jserror($msg);
            }
            $userInfo = model('admin')->getUserInfo($username);
            if($userInfo['lock'] == 1){
                $this->jsonMsg('您输入的用户被锁定已禁止登录');
                exit();
            }
            $password = md5($password);
            if(!empty($userInfo) && $userInfo['password'] == $password){
                model('admin')->upUserInfo($userInfo['id']);
                $this->setLogin($userInfo );
                $this->jsonMsg('登录成功',1);
            }else{
                $this->jsonMsg('您输入的用户名或密码不正确');
            }
        }else{
            $this->display();
        }
    }
    //退出登录
    public function logout(){
        $this->clearLogin( url('index/login') );
    }
    //生成验证码
    public function verify(){
        Image::buildImageVerify();
    }
    //更新缓存
    public function clearCache(){
        api(getApps(), 'clearCache');
        $this->clearCache = TRUE;
        $this->display('index_welcome');
    }
}